package com.zx.idc.backend.gui.api.management.scheduler.job;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.api.management.scheduler.base.BaseFilterAndMonitorJob;
import com.zx.idc.common.attributes.FTPReportDataTypeEnum;
import com.zx.idc.common.attributes.NetCommunicationFileTypeEnum;
import com.zx.idc.common.attributes.SchemaFileNameEnum;
import com.zx.idc.common.constants.CommonConstant;
import com.zx.idc.common.constants.InsStatusConstants;
import com.zx.idc.common.util.DigestUtil;
import com.zx.idc.ds.httplog.entity.MonitorPageSnapshot;
import com.zx.idc.ds.httplog.repository.MonitorPageSnapshotRepository;
import com.zx.idc.ds.ins.entity.InsMonitor;
import com.zx.idc.ds.ins.entity.InsMonitorArchive;
import com.zx.idc.ds.ins.service.IInsMonitorArchiveService;
import com.zx.idc.ds.ins.service.IInsMonitorService;
import com.zx.idc.xml.entity.common.filtermonitor.log.Log;
import com.zx.idc.xml.entity.monitorresult.MonitorResult;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

import static com.zx.idc.common.attributes.SystemAttributes.WEB_SERVICE_MONITOR_LOG_DIR;

/**
 * 监测指令定时上报 20分钟一次
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class ReportMonitorLogDataJob extends BaseFilterAndMonitorJob<IInsMonitorService, InsMonitor, MonitorResult> {

    private static final Logger LOG = LoggerFactory.getLogger(ReportMonitorLogDataJob.class);

    private static final String COLUMN_REPORT = "report";

    @Autowired
    private IInsMonitorArchiveService insMonitorArchiveService;

    @Autowired
    private MonitorPageSnapshotRepository monitorPageSnapshotRepository;

    public ReportMonitorLogDataJob() {
        super(WEB_SERVICE_MONITOR_LOG_DIR,
            SchemaFileNameEnum.ILLEGAL_INFO_MONITOR_RECORD_REPORT,
            FTPReportDataTypeEnum.FTP_ILLEGAL_INFORMATION_MONITORING_RECORD,
            NetCommunicationFileTypeEnum.MONITOR_LOG,
            "监测指令定时上报job");
    }

    /**
     * 定义上报实体
     *
     * @param logs
     * @param idcId
     * @return
     */
    @Override
    protected MonitorResult reportData(List<Log> logs, String idcId) {
        return new MonitorResult().setIdcId(idcId)
            .setTimeStamp(LocalDateTime.now())
            .setLog(logs);
    }

    /**
     * 获取快照
     *
     * @param url
     * @return
     */
    @Override
    protected String snapshot(String url) {
        String md5Digest = DigestUtil.md5Digest(url);
        MonitorPageSnapshot byUrlMd5Value = monitorPageSnapshotRepository.findByUrlMd5Value(md5Digest);
        return byUrlMd5Value.getPageContent();
    }

    @Override
    protected boolean filter(String text) {
        //过滤上报字段为不上报的数据
        String[] split = text.split(CommonConstant.LOG_FIELD_SEPARATOR);
        String commandId = split[0];
        InsMonitorArchive queryCondition = new InsMonitorArchive();
        queryCondition.setId(commandId);
        InsMonitorArchive report = insMonitorArchiveService.selectFirst(new EntityWrapper<>(queryCondition).setSqlSelect(COLUMN_REPORT));
        LOG.debug("监测日志上报，从监测日志存档表查询对比是否需要上传，判定report是否为空：{}", report == null);
        if (report != null) {
            return InsStatusConstants.REPORT_STATUS_UPLOAD.equals(report.getReport());
        }
        return false;
    }

}
